<!--CART STARTS-->
<div id="shopping_cart" class="full_page">
  <h1>Shopping Cart</h1>
  <div class="action_buttonbar">
    <button type="button" title="" class="continue" onclick="window.location=''">Continue Shopping</button>
    <button type="button" title="" class="checkout" id="btn_checkout">Checkout</button>
  </div>
  <div class="cart_table">
    <table class="data-table cart-table" id="shopping-cart-table" cellpadding="0" cellspacing="0">
      <tr>
        <th colspan="2">Products</th>
        <th class="align_center" width="12%">Unit Price</th>
        <th class="align_center" width="10%">Qty</th>
        <th class="align_center" width="12%">Subtotal</th>
        <th class="align_center" width="6%"></th>
      </tr>
	  <?php
		$i=0;
		$subtotal = 0;
		$list_produkid = '';
		if(sizeof($produk > 0) && $produk !== ''){
			foreach($produk as $row_cart){
				echo '<tr>';
				echo '<input type="hidden" id="hide_produkid" value="' . $row_cart->produk_id . '">';
				echo '<td width="10%"><img src="' . base_url() . 'uploads/images/' . $row_cart->produk_sampul . '" style="height:100px; width:auto;"></td>';
				echo '<td class="align_left" width="44%"><a class="pr_name" href="#" onclick="view_singleproduct('. $row_cart->produk_id .')">' . $row_cart->produk_nama . '</a><!--<span class="pr_info">Proin gravida </span>--></td>';
				echo '<td class="align_center vline"><span class="price">Rp. ' . number_format($row_cart->produk_harga,0,',','.') . '</span></td>';
				echo '<td class="align_center vline"><input class="qty_box" type="text" id="cart_jumlahbeli" value="' . $row_cart->qty . '"></td>';
				echo '<td class="align_center vline"><span class="price" id="cart_subharga">Rp. ' . number_format(($row_cart->produk_harga * $row_cart->qty),0,',','.') . '</span></td>';
				echo '<td class="align_center vline"><a href="#" class="remove" id="cart_removeproduct"></a></td>';
				echo '</tr>';
				$subtotal += $row_cart->produk_harga * $row_cart->qty;
				$list_produkid .= $row_cart->produk_id . ',';
				$i++;
			}
		}
	  ?>
    </table>
    <div class="totals">
      <table id="totals-table">
          <!--<tr>
            <td width="60%" colspan="1" class="align_left" ><strong>Subtotal</strong></td>
            <td class="align_right" style=""><strong><span class="price">Rp. <?php echo number_format($subtotal,0,',','.'); ?></span></strong></td>
          </tr>
          <tr>
            <td width="60%" colspan="1" class="align_left" >Shipping cost</td>
            <td class="align_right" style=""><span class="price">0.00</span></td>
          </tr>-->
          <tr>
            <td width="60%" colspan="1" class="align_left total" >Grand Total</td>
            <td class="align_right" style=""><span class="total">Rp. <?php echo number_format($subtotal,0,',','.'); ?></span></td>
          </tr>                
      </table>
    </div>
  </div>
  <div class="action_buttonbar">
    <button type="button" title="" class="continue" onclick="window.location=''">Continue Shopping</button>
    <!--<button type="button"  onClick="parent.location='leisure_checkout.html'" title="" class="checkout">Checkout</button>-->
    <button type="button" title="" class="checkout" id="btn_checkoutbtm">Checkout</button>
  </div>
</div>
<script language="javascript">
	var view_singleproduct = null;
</script>
<script>
$(document).ready(function(){
	function checkout(){
		var subtotal = '<?php echo $subtotal; ?>';
		var list_produkid = '<?php echo substr($list_produkid,0,-1); ?>';
		$.ajax({
			url : 'index.php/c_cart/checkout',
			type : 'POST',
			data : {
				subtotal : subtotal,
				list_produkid : list_produkid
			},success : function(msg){
				var result = eval(msg);
				switch(result){
					case 1:
						alert('Pembelian berhasil');
						my_config.main.load('index.php/c_cart');
					break;
					case 2:
						alert('Saldo tidak cukup');
					break;
					case 3:
						alert('Anda belum login');
					break;
					default:
						alert('Pembelian gagal');
					break;
				}
			}
		});
	}
	$("#btn_checkout").click(function(){
		checkout();
	});
	$("#btn_checkoutbtm").click(function(){
		checkout();
	});
	$("td").find('#cart_jumlahbeli').keyup(function(e){
		var jumlahbeli = $(this).val();
		if(jumlahbeli !== ''){
			var $td = $(this).closest('tr').children('td');
			var produk_id = $(this).closest('tr').find('#hide_produkid').val();
			$.ajax({
				url : 'index.php/c_cart/update_product',
				type : 'POST',
				data : {
					pid : produk_id,
					jumlahbeli : jumlahbeli
				},success : function(msg){
					var result = eval(msg);
					switch(result){
						case 1:
							my_config.main.load('index.php/c_cart');
						break;
						default:
							alert("Item diupdate dihapus");
						break;
					}
				}
			});
		}
	});
	$("td").find('#cart_removeproduct').click(function(e){
		if(window.confirm('Apakah anda yakin menghapus item ini ?')){
			var produk_id = $(this).closest('tr').find('#hide_produkid').val();
			$.ajax({
				url : 'index.php/c_cart/remove_product',
				type : 'POST',
				data : {
					pid : produk_id
				},success : function(msg){
					var result = eval(msg);
					switch(result){
						case 1:
							my_config.main.load('index.php/c_cart');
						break;
						default:
							alert("Item gagal dihapus");
						break;
					}
				}
			});
		}
	});
	function update_topcart(){
		<?php 
		$sumitem = 0;
		$sumprice = 0;
		$list_item = 0;
		if($produk !== ''){
			foreach($produk as $row_pd){
				$sumitem += $row_pd->qty;
				$sumprice += $row_pd->produk_harga * $row_pd->qty;
				$list_item .= '<li><img width="82" height="82" src="'. base_url() .'uploads/images/' . $row_pd->produk_sampul . '"><a href="#">' . $row_pd->produk_nama . '</a> <span class="price">Rp. ' . $row_pd->produk_harga . '</span></li>';
			}
		}	?>
		$("#cart_sumitem").empty();
		$("#cart_sumprice").empty();
		$("#cart_listofitems").empty();
		$("#cart_subtotal").empty();
		$("#cart_sumitem").text('<?php echo $sumitem; ?>');
		$("#cart_sumprice").text('<?php echo $sumprice; ?>');
		$("#cart_listofitems").html('<?php echo $list_item; ?>');
		$("#cart_subtotal").text('<?php echo $sumprice; ?>');
	}
	update_topcart();
	function view_singleproduk(pid){
		my_config.main.load('index.php/c_singleproduct?product_id=' + pid);
		return false;
	}
	view_singleproduct = view_singleproduk;
});
</script>
<!--CART ENDS--> 